﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-01>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：行业分类信息
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaShui.JiChuXX
{
    public partial class HangYeLX : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int HangYeLXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_HangYeLXID"]);
            }
            set
            {
                ViewState["_HangYeLXID"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量
        
        /// <summary>
        /// 行业类型对象
        /// </summary>
        QS_JX_HANGYEFLXX _QS_JX_HANGYEFLXX = new QS_JX_HANGYEFLXX();

        /// <summary>
        /// 行业类型BLL
        /// </summary>
        QS_JX_HANGYEFLXXBLL _QS_JX_HANGYEFLXXBLL = new QS_JX_HANGYEFLXXBLL();

        QS_JX_QUSHUIXX _QS_JX_QUSHUIXX = new QS_JX_QUSHUIXX();

        QS_JX_QUSHUIXXBLL _QS_JX_QUSHUIXXBLL = new QS_JX_QUSHUIXXBLL();

        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        /// <summary>
        /// 操作日志BLL
        /// </summary>
         SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion
        
        #region 事件

        /// <summary>
         /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            { 
                InitData();    
                LoadData();                
            }
            //每次回发时从内存中取得数据源重新绑定,否则列头无法排序
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        //protected override void Render(HtmlTextWriter sender)
        //{
        //    DataTable dt = _YS_JX_HANGYELXBLL.SelectYS_JX_HANGYELXesDynamic("1=0", "", null);
        //    base.Render(sender);
        //}

       

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (this.txtSheShuiDXFLDM.Text == "")
            {
                PageTools.AlertMsg(this, "行业分类编号，不能为空！");
                this.txtSheShuiDXFLDM.Focus();
                return false;
            }
            if (!PageTools.ValidateString(this.txtSheShuiDXFLDM.Text, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "你输入的行业分类编号格式不正确，请输入数字类型！");
                this.txtSheShuiDXFLDM.Focus();
                return false;
            }
            if (this.txtSheShuiDXFLMC.Text == "")
            {
                PageTools.AlertMsg(this, "行业分类名称不能为空！");
                this.txtSheShuiDXFLMC.Focus();
                return false;
            }
            if (this.cmbQuShuiYT.Text == "")
            {
                PageTools.AlertMsg(this, "取水用途名称不能为空！");
                this.cmbQuShuiYT.Focus();
                return false;
            }
           
            return true;
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            if (e.Parameter == "info")
            {
                DataTable dtxx = _SpHelper.P_QSJX_HANGYEXX(id).Tables[0];
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.AddValue(dtxx.Rows[0]["S_FENLEIBH"], dtxx.Rows[0]["S_FENLEIMC"], dtxx.Rows[0]["S_QUSHUIMC"], dtxx.Rows[0]["S_MIAOSHU"]);
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "del")
            {
                this._QS_JX_HANGYEFLXX = _QS_JX_HANGYEFLXXBLL.SelectQS_JX_HANGYEFLXX(id);
                //确认涉水对象代码是否在使用
                //if (new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic(string.Format("s_sheshuidxdm='{0}'", _YS_JX_HANGYELX.S_SHESHUIDXFLDM), null).Rows.Count > 0)
                //{
                //    cr.IsSuccess = false;
                //    cr.IsAlertMsg = true;
                //    cr.Msg = "当前涉水对象代码正在使用,不允许删除！";
                //    e.Result = cr.ToString();
                //    return;
                //}
                //设置逻辑删除
                this._QS_JX_HANGYEFLXX.I_JLZT = -1;
                //执行操作
                //_SYS_LOGBLL.Log<QS_JX_HANGYEFLXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "行业类型", OperateState.Delete, _QS_JX_HANGYEFLXX, null);
                _SYS_LOGBLL.Log<QS_JX_HANGYEFLXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "行业类型", OperateState.Delete, _QS_JX_HANGYEFLXX, null);
                if (_QS_JX_HANGYEFLXXBLL.InsertOrUpdate(_QS_JX_HANGYEFLXX, null))
                {
                   
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                    e.Result = cr.ToString();
                    return;

                }
            }
            else if (e.Parameter == "save")
            {
                OperateState operateState;

                //添加
                if (id == -1)
                {
                    operateState = OperateState.Add;
                    this._QS_JX_HANGYEFLXX.ID = 0;
                    this._QS_JX_HANGYEFLXX.I_JLZT = 0;
                    //判断行业分类编号是否存在
                    if (_QS_JX_HANGYEFLXXBLL.SelectQS_JX_HANGYEFLXXesDynamic(string.Format("I_JLZT=0 and S_FENLEIBH='{0}'", this.txtSheShuiDXFLDM.Text), null).Rows.Count > 0)
                    {
                        cr.IsSuccess = false;
                        cr.IsAlertMsg = true;
                        cr.Msg = "您所输入行业分类编号系统中已经存在使用,请重新输入。";
                        e.Result = cr.ToString();
                        return;
                    }
                }
                //修改
                else
                {
                    //修改操作
                    this._QS_JX_HANGYEFLXX = this._QS_JX_HANGYEFLXXBLL.SelectQS_JX_HANGYEFLXX(id);
                    operateState = OperateState.Modify;
                }
               
                _QS_JX_HANGYEFLXX.S_FENLEIBH = this.txtSheShuiDXFLDM.Text;
                _QS_JX_HANGYEFLXX.S_FENLEIMC = this.txtSheShuiDXFLMC.Text;
                string value=cmbQuShuiYT.Value.ToString();
                //string s_guid = _QS_JX_QUSHUIXXBLL.SelectQS_JX_QUSHUIXXesDynamic(string.Format("S_QUSHUIBH={0}", value), null).Rows[0]["S_GUID"].ToString();
                _QS_JX_HANGYEFLXX.S_GUID = value;
                _QS_JX_HANGYEFLXX.S_MIAOSHU = this.txtMiaoShu.Text;
                //设置操作信息
                _QS_JX_HANGYEFLXX.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                _QS_JX_HANGYEFLXX.D_CAOZUORQ = DateTime.Now;
                _SYS_LOGBLL.Log<QS_JX_HANGYEFLXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "行业类型", operateState, _QS_JX_HANGYEFLXX, null);
                if (_QS_JX_HANGYEFLXXBLL.InsertOrUpdate(_QS_JX_HANGYEFLXX, null))
                {
                    
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "保存成功！";
                    cr.SpecialIndetity = _QS_JX_HANGYEFLXX.ID.ToString();
                    e.Result = cr.ToString();
                    return;
                }
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            DataTable dt = _QS_JX_QUSHUIXXBLL.SelectQS_JX_QUSHUIXXesDynamic("I_JLZT=0", "S_QUSHUIBH");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbQuShuiYT.Items.Add(dt.Rows[i]["S_QUSHUIMC"].ToString(), dt.Rows[i]["S_QUSHUIBH"]);
            }
            cmbQuShuiYT.SelectedIndex = 0;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            this.dtSource = _SpHelper.P_QSJX_HANGYEXX(0).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion        
    }
 
}